<?php

$id=$_GET['id'];
$obj=new mysql($config);

$sql="select * from `goods` where `id`=$id";
$date=$obj->getOne($sql);

// 找出当前商品的缩略图(goodsimg表)
$sql="select * from `goodsimg` where `goods_id`=$id";
$img=$obj->getCondition($sql);


// 找出商品的单选属性(attr goodsattr 表)
$sql="select `goodsattr`.*,`attr`.`attr_name`,`attr`.`attr_type` from `goodsattr` inner join `attr` on `attr`.`id`=`goodsattr`.`attr_id` where `goodsattr`.`goods_id`=$id and (`attr`.`attr_type`=0 or `attr`.`show`=1)";

$_attr=$obj->getCondition($sql);
$attr=array();
foreach ($_attr as $k => $v) {
   $attr[$v['attr_id']][]=$v;
}

// 找出商品的其他属性(attr goodsattr 表)
$sql="select `goodsattr`.`attr_value`,`attr`.`attr_name` from `goodsattr` inner join `attr` on `attr`.`id`=`goodsattr`.`attr_id` where `goodsattr`.`goods_id`=$id and `attr`.`attr_type`=1 and `attr`.`show`=0";
$otherattr=$obj->getCondition($sql);



if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])){
  $goods_id=$_GET['id'];
  // 库存
  $goodsattr_id=implode(',',$_GET['goodsattr_id']);
  $sql="select `number` from `goodsnumber` where `goods_id`=$goods_id and `goodsattr_id`='$goodsattr_id'";
  $date=$obj->getOne($sql);

  // 价钱
  $sql="select `price` from `goodsattr` where `id` in($goodsattr_id)";
  $_price=$obj->getCondition($sql);
  $all='';
  foreach ($_price as $k => $v) {
     $all+=intval($v['price']);
  }
  $row['number']=$date['number'];
  $row['price']=$all;

  echo json_encode($row);


  die;
}



require_once VIEW_PATH.'home/layout.html';

